package cn.edu.hhu.service.Impl;

import cn.edu.hhu.Bean.AttendanceInfo;
import cn.edu.hhu.Bean.AttendanceTask;
import cn.edu.hhu.Bean.Course;
import cn.edu.hhu.Bean.Student;
import cn.edu.hhu.DAO.AttendanceInfoDao;
import cn.edu.hhu.DAO.AttendanceTaskDao;
import cn.edu.hhu.DAO.CourseDao;
import cn.edu.hhu.DAO.StudentDao;
import cn.edu.hhu.DAO.impl.AttendanceInfoDaoImpl;
import cn.edu.hhu.DAO.impl.AttendanceTaskDaoImpl;
import cn.edu.hhu.DAO.impl.CourseDaoImpl;
import cn.edu.hhu.DAO.impl.StudentDaoImpl;
import cn.edu.hhu.service.StudentService;
import org.junit.Test;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.List;

/**
 * @anthor: lddd
 * @date: 2020/5/8 - 9:28
 * @description:
 **/
public class StudentServiceImpl implements StudentService {
    private StudentDao studentDao = new StudentDaoImpl();
    private CourseDao courseDao = new CourseDaoImpl();
    private AttendanceTaskDao taskDao = new AttendanceTaskDaoImpl();
    private AttendanceInfoDao infoDao = new AttendanceInfoDaoImpl();

    /**
     * 登录功能，根据提供的学号和密码验证信息
     *
     * @param student
     * @return 正确返回全部身份信息，错误返回null
     */
    @Override
    public Student login(Student student) {
        Student studentByIdAndPassword = studentDao.findStudentByIdAndPassword(student.getSid(), student.getPwd());
        if (studentByIdAndPassword != null) {
            if (studentByIdAndPassword.isActivation()) {
                return studentByIdAndPassword;
            } else return null;
        } else return null;
    }

    /**
     * 根据学号修改学生信息
     *
     * @param student
     * @return 0表示失败，1成功
     */
    @Override
    public int modifyStudentInformation(Student student) {
        return studentDao.modifyStudentByID(student);
    }

    /**
     * 注册功能，账号存在并且未激活才能注册
     *
     * @param student
     * @return 是否成功
     */
    @Override
    public boolean regist(Student student) {
        //根据学号和姓名查询
        Student studentByIdAndName = studentDao.findStudentByIdAndName(student.getSid(), student.getSname());
        //账号存在，并且未激活
        if (studentByIdAndName != null && !studentByIdAndName.isActivation()) {
            //设置为激活状态，不可二次注册
            student.setActivation(true);
            //存储密码
            studentDao.modifyStudentByID(student);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查询该学生所有课程
     *
     * @param student
     * @return 课程列表
     */
    @Override
    public List<Course> queryAllCourses(Student student) {
        List<Course> courses = courseDao.queryCourseBySid(student.getSid());
        return courses;
    }

    /**
     * 学生完成签到任务，验证学生提供的口令是否正确
     *
     * @param student
     * @param task
     * @return 是否成功
     */
    @Override
    public boolean signin(Student student, AttendanceTask task) {
        //查出数据库中的task
        AttendanceTask sql_task = taskDao.queryTaskByTnum(task.getTnum());
        //对别口令是否正确
        if (((sql_task.getPwd() == null || sql_task.getPwd().length() == 0) && (task.getPwd() == null || task.getPwd().length() == 0)) || sql_task.getPwd().equalsIgnoreCase(task.getPwd())) {
            AttendanceInfo attendanceInfo = new AttendanceInfo();
            attendanceInfo.setTnum(task.getTnum());
            attendanceInfo.setSid(student.getSid());
            attendanceInfo.setSign_in_time(new Timestamp(System.currentTimeMillis()));
            infoDao.insertAttendanceInfo(attendanceInfo);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查询某学生某任务的所有签到信息
     *
     * @param student
     * @param task
     * @return
     */
    @Override
    public AttendanceInfo queryAttendanceInfo(Student student, AttendanceTask task) {
        AttendanceInfo byTnumAndSid = infoDao.findByTnumAndSid(task.getTnum(), student.getSid());
        return byTnumAndSid;
    }

    @Test
    public void test() {
        StudentServiceImpl studentService = new StudentServiceImpl();
    }
}
